/**
 * @author lonun
 * @blog http://www.lonun.com
 */

var parseURL = require('url').parse,
	path = require("path"), 
	fs = require('fs'),
	config = require('../config'),
	WebForm = require('./webform').WebForm;

exports.getWebForm = function(url){
	//var url = req.url;
    //var method = req.method ? req.method.toLowerCase() : 'get';
    var paramStr = null;
    var urlObj = parseURL(url, true);
    // 如果url为：/user/add?id=1 ,则pathname为：/user/add
    var pathname = urlObj.pathname;
    var query = urlObj.query;
    if(pathname.charAt(0)=="/"){
    	paramStr = pathname.substring(1);
    }else{
    	paramStr = pathname;
    }
    if(paramStr.charAt(paramStr.length-1)=="/"){
    	paramStr = paramStr.substring(0,paramStr.length-1);
    }
    var suffix = path.extname(url).toLowerCase();
    var params = paramStr.split("/");
    var actionParam;
    var subApp = params[0];
    var appPath = "";
    var controllerPath = "";
    var controllerName = "";
    var actionName = "";
    var form = null;
    if('.html' == suffix || '.htm' == suffix){//解析html文件的地址，/xx/yy/cc/index.html的文件路径应该是：/app/xx/app/yy/app/cc/index.html
    	for(var i=0;i<params.length;i++){
    		var _param = params[i];
    		if(_param.indexOf(suffix)>-1){//如果有后缀，则去掉后缀
    			_param = _param.replace(suffix, '');
    		}
	    	var tempAppPath = path.join(appPath, "app", _param);
	    	var fullPath = path.join(config.baseDir, tempAppPath);
	    	if(!path.existsSync(fullPath)){//由于去掉了后缀，所以没找到的时候就去寻找文件名为path+后缀的文件，否则就视为找到一个目录
	    		fullPath = path.join(config.baseDir, appPath, "html", _param+suffix);
	    		if(path.existsSync(fullPath)){
	    			form = new WebForm(path.join(appPath, "app"), null, null);
	    			form.setUrl(url);
	    			form.setActionPath(fullPath);
	    			return form;
	    		}else{
	    			console.log("没有找到html文件：/"+tempAppPath);
	    			return null;
	    		}
	    	}else{
	    		appPath = tempAppPath;
	    	}
	    }
    }else{
	    for(var i=0;i<params.length;i++){
	    	var tempAppPath = path.join(appPath, "app", params[i]);
	    	var fullPath = path.join(config.baseDir, tempAppPath);
	    	if(!path.existsSync(fullPath)){//动态请求没有后缀，没找到的时候就去寻找文件名为path+.js的文件，否则就视为找到一个目录
	    		fullPath = fullPath+".js";
	    		if(path.existsSync(fullPath)){
	    			controllerPath = tempAppPath;
	    			controllerName = params[i];
	    			if(params[i+1]){
	    				actionName = params[i+1];
	    			}else{
	    				actionName = "index";
	    			}
	    			form = new WebForm(path.join(appPath, "app"), controllerName, actionName);
	    			form.setUrl(url);
	    			if(params.length=i+2){
	    				form.addParam("key", params[i+1]);
	    			}
	    			if(query){
	    				form.addAllParam(query);
					}
	    			form.setActionPath(fullPath);
	    			//form.setReq(req);
	    			return form;
	    		}else{
	    			console.log("没有找到：/"+tempAppPath);
	    			return null;
	    		}
	    	}else{
	    		appPath = tempAppPath;
	    	}
	    }
    }
    console.log("没找到：/"+appPath);
	return null;
};